package com.tg.controller;

import com.tg.pojo.Friend;
import com.tg.pojo.User;
import com.tg.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/friends")
public class FriendController {
    @Autowired
    private FriendService friendService;

    @GetMapping("/{username}")
    public Result getFriendInfoList(@PathVariable String username) {
        List<User> friendInfoList = friendService.friendInfoList(username);
        return new Result(Code.GET_OK, friendInfoList);
    }

    @DeleteMapping("/{username}/{friendUsername}")
    public Result deleteFriend(@PathVariable String username, @PathVariable String friendUsername) {
        boolean flag = friendService.deleteFriend(username, friendUsername);
        return flag ? new Result(Code.DELETE_OK, null, ResultMessage.DELETE_OK) : new Result(Code.DELETE_ERR, null, ResultMessage.DELETE_ERR);
    }

    @PostMapping
    public Result sendVerification(@RequestBody Friend friend){
        boolean insertCode = friendService.sendVerification(friend);
        return insertCode ? new Result(Code.SAVA_OK, null, ResultMessage.SEND_OK) : new Result(Code.SAVA_ERR, null, ResultMessage.SEND_ERR);
    }

    @GetMapping("/verification/{username}")
    public Result getVerificationList(@PathVariable String username){
        List<Friend> verificationList = friendService.getVerificationList(username);
        return !verificationList.isEmpty() ? new Result(Code.GET_OK,verificationList,ResultMessage.GET_OK) : new Result(Code.GET_ERR,null,ResultMessage.GET_INFO);
    }
}
